<?
include("../connect.php");
$op   = $_GET["op"];
$user_id = $_GET["user_id"];
switch ($op) {
	case "info":
		$fname   =   $_GET["fname"];
		$lname  =   $_GET["lname"];
		$photo  =   $_GET["photo"];
		$gendre    =   $_GET["gendre"];
		$tel    =   $_GET["tel"];
		$query = "UPDATE users SET name='$fname', lname='$lname', photo='$photo', sex='$gendre', tel='$tel' WHERE id_user=$user_id";
		/*$query = "UPDATE users SET `name` = 'a',
`lname` = 'aa',
`photo` = '333',
`sex` = '0',
`tel` = '333' WHERE `users`.`id_user` =3 LIMIT 1 ;";*/
		break;
	case "add_cursus":	
		$level = $_GET["level"];
		$etablissement = $_GET["etablissement"];
		$year = $_GET["year"];
		$description = $_GET["description"];
		$user_id = $_GET["user_id"];
		$query = "INSERT INTO cursus VALUES('','$level','$etablissement','$year','$description','$user_id') ";
		break;
	case "show_cursus":
		$user_id = $_GET["user_id"];
		$query = "SELECT * FROM cursus WHERE id_user_fkn = $user_id";
		break;
	case "delete_cursus":
		$cursus_id = $_GET["cursus_id"];
		$query  = "DELETE FROM cursus WHERE id_cursus = $cursus_id";
		break;
//----------experience fnc	
	case "show_experience":
		$user_id = $_GET["user_id"];
		$query = "SELECT * FROM experience WHERE id_user_fkn = $user_id";
		break;
	case "add_exp":	
		$post = $_GET["post"];
		$company = $_GET["company"];
		$year = $_GET["year"];
		$resp = $_GET["resp"];
		$user_id = $_GET["user_id"];
		$query = "INSERT INTO experience VALUES('','$post','$company','$year','$resp','$user_id') ";
		break;
	case "delete_exp":
		$exp_id = $_GET["exp_id"];
		$query  = "DELETE FROM experience WHERE id_experience = $exp_id";
		break;
//-------------albums fnc
	case "add_album":	
		$album = $_GET["album"];
		$is_pub = $_GET["is_pub"];
		$user_id = $_GET["user_id"];
		$query = "INSERT INTO albums VALUES('','$album','$is_pub','$user_id') ";
		break;
	case "delete_album":
		$album_id = $_GET["album_id"];
		$query  = "DELETE FROM albums WHERE id_album = $album_id";
		break;
	case "show_album":
		$user_id = $_GET["user_id"];
		$query = "SELECT * FROM albums WHERE id_user_fkn = $user_id";
		break;
	//show photos
	case "get_photos":
		$album_id = $_GET["album_id"];
		$query = "SELECT * FROM photos WHERE id_album_fkn = $album_id ";
		break;
	//show photo detail
	case "show_photo_detail":
		$div_id   = $_GET["div_id"];
		$photo_id = $_GET["photo_id"];
		$query = "SELECT * FROM photos WHERE id_photo = $photo_id ";
		break;
	case "modify_photo":
		$photo_id = $_GET["id_photo"];
		$title = $_GET["title"];
		$desc =  $_GET["desc"];
		$query = "UPDATE photos SET title = '$title' , description = '$desc' where id_photo = $photo_id ";
		break;
	case "delete_photo":
		$photo_id = $_GET["id_photo"];
		$album_id = $_GET["id_album"];
		$query = "DELETE FROM photos WHERE id_photo = $photo_id";
	default:
		break;
}

$res = mysql_query($query);
if( (($op == "delete_cursus") || ($op == "delete_exp") || ($op == "delete_album") )  && ($res) ){
 $user_id = $_GET["id_user"];	
 $array = "{'user_id':'$user_id '}";	
}else
if(($res) && ($op == "delete_photo"))
{
 $array = "{'album_id':'$album_id'}";		
}
else
if(($op == "show_cursus") && ($res) ){
	if(!$res) {
		$array = '{ "status":"error",'.
				'"sql":"'.$query.'"}';
	} else {
	
		$results = array();
			
		while($data = mysql_fetch_array($res)) {
			
			$results[] = 	'{'.
							'"id_cursus":"'.$data["id_cursus"].'",'.
							'"level":"'.$data["level"].'",'.
							'"etablissement":"'.utf8_encode(trim($data["etablissement"])).'",'.
							'"year":"'.trim($data["year"]).'",'.
							'"description":"'.utf8_encode(trim($data["description"])).'",'.
							'"id_user_fkn":"'.$data["id_user_fkn"].'"'.
							'}';	
		}
		
		$array = "[" . join(",",$results) . "]";
	}
}else
if(($op == "show_experience") && ($res) ){
	if(!$res) {
		$array = '{ "status":"error",'.
				'"sql":"'.$query.'"}';
	} else {
	
		$results = array();
			
		while($data = mysql_fetch_array($res)) {
			
			$results[] = 	'{'.
							'"id_exp":"'.$data["id_experience"].'",'.
							'"post":"'.$data["post"].'",'.
							'"company":"'.utf8_encode(trim($data["company"])).'",'.
							'"year":"'.trim($data["year"]).'",'.
							'"res":"'.utf8_encode(trim($data["responsibility"])).'",'.
							'"id_user_fkn":"'.$data["id_user_fkn"].'"'.
							'}';	
		}
		
		$array = "[" . join(",",$results) . "]";
	}
}
else
if(($op == "show_album") && ($res) ){
	if(!$res) {
		$array = '{ "status":"error",'.
				'"sql":"'.$query.'"}';
	} else {
	
		$results = array();
			
		while($data = mysql_fetch_array($res)) {
			
			$results[] = 	'{'.
							'"id_album":"'.$data["id_album"].'",'.
							'"is_pub":"'.$data["public"].'",'.
							'"album":"'.utf8_encode(trim($data["name"])).'",'.
							'"id_user_fkn":"'.$data["id_user_fkn"].'"'.
							'}';	
		}
		
		$array = "[" . join(",",$results) . "]";
	}
}
else
if(($op == "get_photos") && ($res) ){
	if(!$res) {
		$array = '{ "status":"error",'.
				'"sql":"'.$query.'"}';
	} else {
	   if(mysql_num_rows($res) == 0)
		   $array = '[{'.
							'"id_album":"'.$album_id.'",'.
							'"id_photo":"nophoto",'.
							'"is_pub":"nophoto",'.
							'"description":"nophoto",'.
							'"path":"nophoto"'.
							'}]';	
	   else{
		$results = array();
			
		while($data = mysql_fetch_array($res)) {
			
			$results[] = 	'{'.
							'"id_album":"'.$data["id_album_fkn"].'",'.
							'"id_photo":"'.$data["id_photo"].'",'.
							'"is_pub":"'.$data["public"].'",'.
							'"description":"'.utf8_encode(trim($data["description"])).'",'.
							'"path":"'.utf8_encode(trim($data["path"])).'"}';	
		}
		
		$array = "[" . join(",",$results) . "]";
	 }
	}
}
else
if(($op == "show_photo_detail") && ($res) ){
	if(!$res) {
		$array = '{ "status":"error",'.
				'"sql":"'.$query.'"}';
	} else {
		   $data = mysql_fetch_array($res);	
		   $array = '{'.
							'"id_div":"'.$div_id.'",'.
							'"id_photo":"'.$photo_id.'",'.
							'"path":"'.utf8_encode(trim($data["path"])).'",'.
							'"title":"'.utf8_encode(trim($data["title"])).'",'.
							'"description":"'.utf8_encode(trim($data["description"])).'"'.
							'}';	
	}
}
else
if($res){
	$array = "{'res':'yes'}";
}
else {
	$array = "{'res':'no'}";
}

echo $array;


?>